<?

function get_tt_best_deal($id)
{
	$id = (int)$id;
	if(!$id) return false;
	
	$sql = "SELECT s2.value as plan_id
			FROM stores s1, stores_takeatour s2
			WHERE s1.reseller_id = '$id' AND s1.store_id = s2.store_id AND s2.name = 'best_deal'";
	if($GLOBALS['master_db']->query($sql))
	{
		if($bestDeal = $GLOBALS['master_db']->getValue())
			return $bestDeal;
		else return false;
	}else exit("Temporary Error!");
}

function get_tt_steps($id)
{
	$id = (int)$id;
	if(!$id) return false;
	
	$sql = "SELECT s2.name as step, s2.value as variant
			FROM stores s1, stores_takeatour s2
			WHERE s1.reseller_id = '$id' AND s1.store_id = s2.store_id and s2.name like 'step%'";
	if($GLOBALS['master_db']->query($sql))
	{
		if($steps = $GLOBALS['master_db']->getAll())
		{
			foreach($steps as $key=>$value)
				$steps[$value[step]] = $value[variant];
			return $steps;
		}else return false;
	}else exit("Temporary Error!");
}

function set_tt_best_deal($id)
{
	$id = (int)$id;
	if(!$id) return false;
	
	$bestDeal = (int)$GLOBALS['master_db']->escapeStr($_POST['bestDeal']);
	if(!$bestDeal)
	{
		$GLOBALS['errorMsg'] = "Please select a plan!";
		return false;
	}
	
	if(get_tt_best_deal($id))
	{
		$sql = "UPDATE stores_takeatour SET value='".$bestDeal."' WHERE store_id=(SELECT store_id FROM stores WHERE reseller_id = $id) and name='best_deal'";
	}else{
		$sql = "INSERT INTO stores_takeatour (store_id, name, value) VALUES ((SELECT store_id FROM stores WHERE reseller_id = $id), 'best_deal', '".$bestDeal."')";
	}
	if(!$GLOBALS['master_db']->query($sql))
	{
		$GLOBALS['errorMsg'] = "Impossible update of best deal!";
		return false;
	}else{
		$GLOBALS['okMsg'] = "Best deal updated!";
		return true;
	}
}

function set_tt_steps($id)
{
	$id = (int)$id;
	if(!$id) return false;
	
	$step0 = (int)$GLOBALS['master_db']->escapeStr($_POST['step0']);
	if(!$step0) $step0 = 1;
	
	$step1 = (int)$GLOBALS['master_db']->escapeStr($_POST['step1']);
	if(!$step1) $step1 = 1;
	
	$step2 = (int)$GLOBALS['master_db']->escapeStr($_POST['step2']);
	if(!$step2) $step2 = 1;
	
	$step3 = (int)$GLOBALS['master_db']->escapeStr($_POST['step3']);
	if(!$step3) $step3 = 1;
	
	$step4 = (int)$GLOBALS['master_db']->escapeStr($_POST['step4']);
	if(!$step4) $step4 = 1;
	
	if(get_tt_steps($id))
	{
		$sql = "UPDATE stores_takeatour SET value='".$step0."' WHERE store_id=(SELECT store_id FROM stores WHERE reseller_id = $id) and name='step0';";
		$sql .= "UPDATE stores_takeatour SET value='".$step1."' WHERE store_id=(SELECT store_id FROM stores WHERE reseller_id = $id) and name='step1';";
		$sql .= "UPDATE stores_takeatour SET value='".$step2."' WHERE store_id=(SELECT store_id FROM stores WHERE reseller_id = $id) and name='step2';";
		$sql .= "UPDATE stores_takeatour SET value='".$step3."' WHERE store_id=(SELECT store_id FROM stores WHERE reseller_id = $id) and name='step3';";
		$sql .= "UPDATE stores_takeatour SET value='".$step4."' WHERE store_id=(SELECT store_id FROM stores WHERE reseller_id = $id) and name='step4';";
	}else{
		$sql = "INSERT INTO stores_takeatour (store_id, name, value) VALUES ((SELECT store_id FROM stores WHERE reseller_id = $id), 'step0', '".$step0."');";
		$sql .= "INSERT INTO stores_takeatour (store_id, name, value) VALUES ((SELECT store_id FROM stores WHERE reseller_id = $id), 'step1', '".$step1."');";
		$sql .= "INSERT INTO stores_takeatour (store_id, name, value) VALUES ((SELECT store_id FROM stores WHERE reseller_id = $id), 'step2', '".$step2."');";
		$sql .= "INSERT INTO stores_takeatour (store_id, name, value) VALUES ((SELECT store_id FROM stores WHERE reseller_id = $id), 'step3', '".$step3."');";
		$sql .= "INSERT INTO stores_takeatour (store_id, name, value) VALUES ((SELECT store_id FROM stores WHERE reseller_id = $id), 'step4', '".$step4."');";
	}
	if(!$GLOBALS['master_db']->query($sql))
	{
		$GLOBALS['errorMsg'] = "Impossible update of step variants!";
		return false;
	}else{
		$GLOBALS['okMsg'] = "Step variants updated!";
		return true;
	}
}


$id = (int)$id;
if(!$id)  exit("Temporary Error!");

//--- get plans
$sql = "SELECT product_id,name 
		FROM products 
		WHERE reseller_id='$id' AND type='plan' and offer_product = 't'
		ORDER BY seq asc";
		
if($GLOBALS['master_db']->query($sql))
{
	$plans = $GLOBALS['master_db']->getAll();
}else exit("Temporary Error!");
//----------------------------

$option = $_POST['option'];
if($option == 'BestDeal')
{
	set_tt_best_deal($id);
}elseif($option == 'TourSteps'){
	set_tt_steps($id);
}


$best = get_tt_best_deal($id);
$steps = get_tt_steps($id);

?>
<div class="text" align="left"><b>Edit Take a Tour</b>
</div><br>
<? // errorMsg an okMsg print------------
if($errorMsg) {
	include ('errorMsg.html');
}

if($okMsg) {
	//$okMsg = translate('successufully_changed_aboutus');
	include ('okMsg.html');
}
//-----------------------------------------
?>
      <br>
	<form name="form1" method="post" action="<?=$_SERVER[PHP_SELF].'?'.$_SERVER[QUERY_STRING]?>">
	<table width="200" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#FFFFFF" class="bTable">
      <tr>
        <th colspan="2">Best Deal Plan</th>
      </tr>
	  <? for($i=0;$i<count($plans);$i++){ ?>
	  <tr>
        <td align="right" width="50%"><input type="radio" name="bestDeal" value="<?=$plans[$i]['product_id']?>" <? if($best == $plans[$i]['product_id']) print "checked"; ?>></td>
        <td align="left" width="50%"><?=$plans[$i]['name']?></td>
      </tr>
	  <? } ?>
	  <tr>
        <td colspan="2" align="center">
		<input type="hidden" name="option" value="BestDeal">
		<input name="selectBestDeal" type="submit" value="Save Selected"></td>
      </tr>
	 </table>
	 </form>
	 <br>
	<form name="form1" method="post" action="<?=$_SERVER[PHP_SELF].'?'.$_SERVER[QUERY_STRING]?>">
	<table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#FFFFFF" class="bTable">
      <tr>
        <th>Step Variants</th>
      </tr>
	  <tr>
        <td>
		<table  border="0" cellpadding="2" cellspacing="1" bgcolor="#FFFFFF">
		  <tr>
		    <th colspan="2">Introduction</th>
		  </tr>
		  <tr>
		    <td width="150"><img src="images/take_a_tour/15/step0_1.gif" border="0"></td>
		    <td width="150"><img src="images/take_a_tour/15/step0_2.gif" border="0"></td>
		  </tr>
		  <tr>
		    <td align="center"><input type="radio" name="step0" value="1" <? if(!$steps['step0'] or $steps['step0'] == 1) print "checked"; ?>></td>
		    <td align="center"><input type="radio" name="step0" value="2" <? if($steps['step0'] == 2) print "checked"; ?>></td>
		  </tr>
		</table>
		</td>
      </tr>
	  <tr>
        <td>
		<table  border="0" cellpadding="2" cellspacing="1" bgcolor="#FFFFFF">
		  <tr>
		    <th>Step 1</th>
		  </tr>
		  <tr>
		    <td width="150"><img src="images/take_a_tour/15/step1_1.gif" border="0"></td>
		  </tr>
		  <tr>
		    <td align="center"><input type="radio" name="step1" value="1" <? if(!$steps['step1'] or $steps['step1'] == 1) print "checked"; ?>></td>
		  </tr>
		</table>
		</td>
      </tr>
	  <tr>
        <td>
		<table width="100%" border="0" cellpadding="2" cellspacing="1" bgcolor="#FFFFFF">
		  <tr>
		    <th colspan="3">Step 2</th>
		  </tr>
		  <tr>
		    <td width="150"><img src="images/take_a_tour/15/step2_1.gif" border="0"></td>
		    <td width="150"><img src="images/take_a_tour/15/step2_2.gif" border="0"></td>
		    <td width="150"><img src="images/take_a_tour/15/step2_3.gif" border="0"></td>
		  </tr>
		  <tr>
		    <td align="center"><input type="radio" name="step2" value="1" <? if(!$steps['step2'] or $steps['step2'] == 1) print "checked"; ?>></td>
		    <td align="center"><input type="radio" name="step2" value="2" <? if($steps['step2'] == 2) print "checked"; ?>></td>
		    <td align="center"><input type="radio" name="step2" value="3" <? if($steps['step2'] == 3) print "checked"; ?>></td>
		  </tr>
		</table>
		</td>
      </tr>
	  <tr>
        <td>
		<table  border="0" cellpadding="2" cellspacing="1" bgcolor="#FFFFFF">
		  <tr>
		    <th colspan="2">Step 3</th>
		  </tr>
		  <tr>
		    <td width="150"><img src="images/take_a_tour/15/step3_1.gif" border="0"></td>
		    <td width="150"><img src="images/take_a_tour/15/step3_2.gif" border="0"></td>
		  </tr>
		  <tr>
		    <td align="center"><input type="radio" name="step3" value="1" <? if(!$steps['step3'] or $steps['step3'] == 1) print "checked"; ?>></td>
		    <td align="center"><input type="radio" name="step3" value="2" <? if($steps['step3'] == 2) print "checked"; ?>></td>
		  </tr>
		</table>
		</td>
      </tr>
	  <tr>
        <td>
		<table  border="0" cellpadding="2" cellspacing="1" bgcolor="#FFFFFF">
		  <tr>
		    <th colspan="2">Step 4</th>
		  </tr>
		  <tr>
		    <td width="150"><img src="images/take_a_tour/15/step4_1.gif" border="0"></td>
		    <td width="150"><img src="images/take_a_tour/15/step4_2.gif" border="0"></td>
		  </tr>
		  <tr>
		    <td align="center"><input type="radio" name="step4" value="1" <? if(!$steps['step4'] or $steps['step4'] == 1) print "checked"; ?>></td>
		    <td align="center"><input type="radio" name="step4" value="2" <? if($steps['step4'] == 2) print "checked"; ?>></td>
		  </tr>
		</table>
		</td>
      </tr>
	  <tr>
        <td align="center">
		<input type="hidden" name="option" value="TourSteps">
		<input name="SaveTourSteps" type="submit" value="Save Selected"></td>
      </tr>
	 </table>
	 </form>